package com.asa.ui.J.A;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.ui.frame.AlgoVisHelper;

public class AlgoFrame extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3035088527551930125L;
	private int canvasWidth;//画布的宽
	private int canvasHeight;//画布的长
	private FractalData data;//分钱数组
   public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	public void render(FractalData data){
		this.data=data;
		repaint();
	}
   public AlgoFrame(String title,int  canvasWidth, int canvasHeight){
	  super(title);
	  this.canvasWidth=canvasWidth;
	  this.canvasHeight=canvasHeight;
	  setDefaultCloseOperation(EXIT_ON_CLOSE);
	  setResizable(false);//关闭可拖动
	  setVisible(true);
	  AlgoCanvas algoCanvas = new AlgoCanvas();
	  setContentPane(algoCanvas);
	  pack();
   }
   //设置面板JPanel
   class AlgoCanvas extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6056196800598676936L;
    private  AlgoCanvas(){
    	//开启双缓存
    	super(true);
    }
	@Override
	//在面板上绘制内容
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d=(Graphics2D) g;
		AlgoVisHelper.setRenderingHints(g2d);//设置抗锯齿
		AlgoVisHelper.setStrokeWidth(g2d, 1);//设置边的宽度为5
		
		//具体绘制
		drawFractal(g2d,0,0,canvasWidth,canvasHeight,0);
		
		
		
		
	}
	
	
	
	
	
	private void drawFractal(Graphics2D g2d, int x, int y, int w, int h,int depth) {
		// TODO Auto-generated method stub
		if (depth == data.getDepth()) {
			AlgoVisHelper.setColor(g2d, Color.PINK);
			AlgoVisHelper.fillReactangle(g2d, x, y, w, h);
			return;
		}
		
		if (w<=1||h<=1) {
			AlgoVisHelper.setColor(g2d, Color.PINK);
			AlgoVisHelper.fillReactangle(g2d, x, y, Math.max(w, 1), Math.max(h, 1));
			return;
		}
		
		int w_3 = w/3;
		int h_3 = h/3;
		
		drawFractal(g2d, x, y, w_3, h_3, depth+1);
		drawFractal(g2d, x + 2*w_3, y, w_3, h_3, depth+1);
		drawFractal(g2d, x + w_3, y+h_3, w_3, h_3, depth+1);
		drawFractal(g2d, x , y + 2*h_3, w_3, h_3, depth+1);
		drawFractal(g2d, x + 2*w_3, y + 2*h_3, w_3, h_3, depth+1);

		
		
	}

	
	
	
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(canvasWidth,canvasHeight);
	}
   }
  
}
